﻿////////////////////////////////////////////////////////////////////////////////////////////////////
// file:	DBConnection\UserCreate.cs
//
// summary:	Implements the user create class
////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using MySql.Data.MySqlClient;

namespace Usermanagment.DBConnection
{
   ////////////////////////////////////////////////////////////////////////////////////////////////////
   /// <summary>
   ///  User Creation Class which is responsible for adding new user to the MYSQL Database.
   /// </summary>
   ///
   /// <remarks>	Avishai, 10/09/2012. </remarks>
   ////////////////////////////////////////////////////////////////////////////////////////////////////

   public class UserCreate
    {
        /// <summary>	The Database connection object - dbc. </summary>
        private DBConnect dbc;
		/// <summary>	The message. </summary>
		private String message;
		private string playersTable;
		private string statsTable;

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Initializes a new instance of the  <see cref="UserCreate"/> class. </summary>
		///
		/// <remarks>	Avishai, 10/09/2012. </remarks>
		///
		/// <param name="dbc">	The DBC. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public UserCreate(DBConnect dbc)
        {
            this.dbc = dbc;
			this.playersTable = dbc.playerDB;
			this.statsTable = dbc.playerStats;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>	Inserts a user. </summary>
        ///
        /// <remarks>	Avishai, 10/09/2012. </remarks>
        ///
        /// <param name="username">	The username. </param>
        /// <param name="password">	The password. </param>
        ///
        /// <returns>	true if it succeeds, false if it fails. </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public Boolean insertUser(String username, String password)
        {
            if (dbc.Connected())
			{
				String command = "INSERT INTO `" + playersTable + "`(Username,Password,DateReg,CanPlay,CanConnect,Chips) VALUES('" + username + "','" + password + "'," + CurrentTimeMillis() + "," + 1 + "," + 1 + "," + 2000 + ")";
				MySqlCommand cmd = new MySqlCommand(command, dbc.getCon());                 
                cmd.ExecuteNonQuery();
				cmd.CommandText = "INSERT INTO `" + statsTable + "`(Username,Wins,Loss,Ratio) VALUES('" + username + "'," + 0 + "," + 0 + ",'0')";
				cmd.ExecuteNonQuery();
				message = "Added";
                return true;
            }
            else
            {
                message = "Error Connecting. Please connect to the DB first!";
                return false;

            }
        }

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the current time millis. </summary>
		///
		/// <remarks>	Avishai, 10/09/2012. </remarks>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public Int32 CurrentTimeMillis()
		{
		   DateTime staticDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
           TimeSpan timeSpan = DateTime.UtcNow - staticDate;
           return Convert.ToInt32(timeSpan.TotalSeconds);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the stat. </summary>
		///
		/// <remarks>	Avishai, 10/09/2012. </remarks>
		///
		/// <returns>	The stat. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public String getStat()
		{
			return message;
		}

    }
}
